<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <!-- <el-form-item label="关联的租赁申请ID" prop="applicationId">
        <el-input
          v-model="queryParams.applicationId"
          placeholder="请输入关联的租赁申请ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="人才姓名" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入人才姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="公寓地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入公寓地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同开始日期" prop="startDate">
        <el-date-picker
          clearable
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择合同开始日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同结束日期" prop="endDate">
        <el-date-picker
          clearable
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择合同结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable>
          <el-option
            v-for="dict in dict.type.contract_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="租金金额" prop="rentAmount">
        <el-input
          v-model="queryParams.rentAmount"
          placeholder="请输入租金金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="签订日期" prop="signDate">
        <el-date-picker
          clearable
          v-model="queryParams.signDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择签订日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同状态" prop="contractStatus">
        <el-select v-model="queryParams.contractStatus" placeholder="请选择合同状态" clearable>
          <el-option
            v-for="dict in dict.type.contract_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
          搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['room:bills:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['room:bills:export']"
        >
          导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="billsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="租金账单唯一标识符" align="center" prop="billId" /> -->
      <el-table-column label="人才姓名" align="center" prop="nickName" />
      <el-table-column label="人才类型" align="center" prop="eliteType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.elite_type" :value="scope.row.eliteType" />
        </template>
      </el-table-column>
      <el-table-column label="联系方式" align="center" prop="phonenumber" />
      <el-table-column label="公寓地址" align="center" prop="address" />
      <el-table-column label="合同开始日期" align="center" prop="startDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束日期" align="center" prop="endDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同类型" align="center" prop="contractType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contract_type" :value="scope.row.contractType" />
        </template>
      </el-table-column>
      <el-table-column label="租金金额" align="center" prop="rentAmount" />
      <el-table-column label="签订日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="contractStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.contract_status" :value="scope.row.contractStatus" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row, 2)"
            v-hasPermi="['room:bills:edit']"
          >
            查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 3)"
            v-if="scope.row.contractStatus == 0"
            v-hasPermi="['room:bills:edit']"
          >
            提交
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row, 4)"
            v-if="scope.row.contractStatus == 1"
            v-hasPermi="['room:bills:edit']"
          >
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租赁账单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="600px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <!-- <el-form-item label="关联的租赁申请ID" prop="applicationId">
          <el-input v-model="form.applicationId" placeholder="请输入关联的租赁申请ID" disabled />
        </el-form-item> -->
        <el-form-item label="人才姓名" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入人才姓名" disabled />
        </el-form-item>
        <el-form-item label="联系方式" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入联系方式" disabled />
        </el-form-item>
        <!-- <el-form-item label="公寓Id" prop="apartmentId">
          <el-input v-model="form.apartmentId" placeholder="请输入公寓Id" disabled />
        </el-form-item> -->
        <el-form-item label="公寓地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入公寓地址" disabled />
        </el-form-item>
        <el-form-item label="合同开始日期" prop="startDate">
          <el-date-picker
            clearable
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同开始日期"
            :disabled="openType == 2 || openType == 4"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束日期" prop="endDate">
          <el-date-picker
            clearable
            v-model="form.endDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同结束日期"
            :disabled="openType == 2 || openType == 4"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型" disabled>
            <el-option
              v-for="dict in dict.type.contract_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="租金金额" prop="rentAmount">
          <el-input v-model="form.rentAmount" placeholder="请输入租金金额" disabled />
        </el-form-item>
        <el-form-item label="签订日期" prop="signDate">
          <el-date-picker
            clearable
            v-model="form.signDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择签订日期"
            :disabled="openType == 2 || openType == 4"
          ></el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="合同状态" prop="contractStatus">
          <el-select v-model="form.contractStatus" placeholder="请选择合同状态" disabled>
            <el-option
              v-for="dict in dict.type.contract_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="租赁附件" prop="leaseAttachments">
          <file-upload
            v-model="form.leaseAttachments"
            :fileType="['doc', 'docx', 'xls', 'xlsx', 'ppt', 'txt', 'pdf', 'png', 'jpg', 'jpeg']"
            :dis="openType == 2"
          />
        </el-form-item>
        <!-- <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" :disabled="openType == 2" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="openType == 3 || openType == 4">
          确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBills,
  getBills,
  delBills,
  addBills,
  updateBills,
  addRentalBillsMaterial
} from '@/api/system/bills'

export default {
  name: 'Bills',
  dicts: ['contract_type', 'contract_status', 'elite_type'],
  data() {
    return {
      openType: 1,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 租赁账单表格数据
      billsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationId: null,
        nickName: null,
        phonenumber: null,
        apartmentId: null,
        address: null,
        startDate: null,
        endDate: null,
        contractType: null,
        rentAmount: null,
        signDate: null,
        contractStatus: null,
        leaseAttachments: null,
        createBy: null,
        createTime: null,
        updateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        startDate: [{ required: true, message: '合同开始日期不能为空', trigger: 'blur' }],
        endDate: [{ required: true, message: '合同结束日期不能为空', trigger: 'blur' }],
        signDate: [{ required: true, message: '签订日期不能为空', trigger: 'blur' }],
        leaseAttachments: [{ required: true, message: '租赁附件不能为空', trigger: 'blur' }]
        // remark: [{ required: true, message: '备注不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询租赁账单列表 */
    getList() {
      this.loading = true
      listBills(this.queryParams).then((response) => {
        this.billsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        applicationId: null,
        nickName: null,
        phonenumber: null,
        apartmentId: null,
        address: null,
        startDate: null,
        endDate: null,
        contractType: null,
        rentAmount: null,
        signDate: null,
        contractStatus: null,
        leaseAttachments: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.billId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.openType = 1
      this.open = true
      this.title = '添加租赁账单'
    },
    /** 修改按钮操作 */
    handleUpdate(row, openType) {
      this.reset()
      this.openType = openType
      const billId = row.billId || this.ids
      getBills(billId).then((response) => {
        this.form = response.data
        this.open = true
        this.title =
          openType == 2 ? '查看租赁账单' : openType == 4 ? '修改租赁账单' : '提交租赁账单'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.billId != null) {
            addRentalBillsMaterial(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addBills(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const billIds = row.billId || this.ids
      this.$modal
        .confirm('是否确认删除租赁账单编号为"' + billIds + '"的数据项？')
        .then(function () {
          return delBills(billIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'room/bills/export',
        {
          ...this.queryParams
        },
        `bills_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
